<template>
  <el-dialog
    title="外框尺寸"
    :visible.sync="dialogVisible"
    width="900px"
    :append-to-body="true"
  >
    <div class="img-list">
      <div
        v-for="(item, index) in list"
        :key="index"
        class="img-item"
        @click="handleSelect(item)"
      >
        <div class="img-box" :style="{ backgroundImage: `url(${item.url})` }" />
        <div class="img-meta">{{ item.title }}</div>
        <div v-if="selected?.id === item.id" class="selected">
          <div class="index">{{ index + 1 }}</div>
        </div>
      </div>
    </div>
    <el-form label-width="70px" :inline="true" size="mini">
      <el-form-item label="宽(mm)">
        <el-input-number v-model="width" controls-position="right" :min="1" />
      </el-form-item>
      <el-form-item label="高(mm)">
        <el-input-number v-model="height" controls-position="right" :min="1" />
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button
        type="primary"
        :disabled="!width || !height || !selected"
        @click="handleOk"
      >
        确 定
      </el-button>
    </span>
  </el-dialog>
</template>

<script>
import { mapGetters } from 'vuex'

export default {
  name: 'ModalSectionPicker',
  props: {
    value: {
      type: Boolean,
      default: false,
    },
  },
  data: () => {
    return {
      selected: null,
      width: '',
      height: '',
      list: [
        {
          id: 1,
          url: '/default-img.png',
          title: '普通',
          name: 'Section',
        },
        {
          id: 2,
          url: '/default-img2.png',
          title: '门联窗',
          name: 'Section',
        },
        {
          id: 3,
          url: '/default-img.png',
          title: '肯德基门',
          name: 'Section',
        },
      ],
    }
  },
  computed: {
    ...mapGetters(['selectedSection', 'components']),
    dialogVisible: {
      get() {
        return this.value
      },
      set(v) {
        this.$emit('input', v)
      },
    },
  },
  methods: {
    handleSelect(item) {
      this.selected = item
    },
    handleOk() {
      this.dialogVisible = false
      const { width, height, selected } = this
      this.$emit('change', { width, height, selected })
      this.selected = null
    },
  },
}
</script>

<style scoped>
.img-list {
  overflow-y: auto;
}
.img-item {
  position: relative;
  float: left;
  width: 105px;
  height: 105px;
  margin-right: 10px;
  margin-bottom: 30px;
  cursor: pointer;
}
.img-list .img-box {
  background: #ccc;
  width: 105px;
  height: 105px;
  background-size: cover;
  background-position: 50% 50%;
}
.img-meta {
  position: absolute;
  width: 105px;
  height: 25px;
  font-size: 12px;
  line-height: 25px;
  color: #fff;
  text-align: center;
  background: rgba(0, 0, 0, 0.2);
  bottom: 0;
}
.selected {
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  border: 2px solid #07d;
  position: absolute;
  box-sizing: border-box;
}
.selected:after {
  position: absolute;
  display: block;
  content: '';
  right: 0;
  top: 0;
  border-color: #07d #07d transparent transparent;
  border-style: solid;
  border-width: 14px;
  z-index: 1;
}
.selected .index {
  position: absolute;
  top: 0;
  right: 0;
  z-index: 2;
  font-size: 12px;
  color: #fff;
  width: 13px;
  text-align: center;
  height: 15px;
  line-height: 1;
}
</style>
